<?php
include_once '../admin/include/group_extends.class.php';
$group_user = new group_user();
$do = isset($do) ? $do : 'manage';
$gid = isset($gid) ? $gid : 0;
$ginfo = $group->get_group_by_id($gid);
$forward = get_forward('/admin/group.php');
if(!isset($ginfo['g_id']))
{
	showmessage('你要编辑的组不存在！');
}
$view_list[$gid] = isset($view_list[$gid]) ? $view_list[$gid] : '';
if(check_perms('G004',$view_list[$gid])===false)
{
	if(isset($edufepm) && $edufepm=='iframe')
	{
		$response->alert('您没有权限进行此操作！');
		$response->redirect($base_dir);
		$response->execute();
	}
	else 
	{
		showmessage('您没有权限进行此操作！');
	}
}
switch ($do)
{
	case 'add':
		if(!isset($view_list[$gid]) || check_perms('G005',$view_list[$gid])===false)
		{
			if(isset($edufepm) && $edufepm=='iframe')
			{
				$response->alert('您没有权限进行此操作！');
				$response->redirect($base_dir);
				$response->execute();
			}
			else 
			{
				showmessage('您没有权限进行此操作！');
			}
		}
		if(isset($_POST['add_user']))
		{
			$flag = $group_user->check_user_in_group($add_user,$gid);
			if($flag === true)
			{
				$response->alert('此管理员已经是该组成员');
				$response->execute();
			}
			if(empty($add_user))
			{
				$response->alert('请选择管理员！');
				$response->execute();
			}
			$res = $group_user->add_user($add_user,$gid);
			if($res === false)
			{
				$response->alert('添加失败！');
			}
			else 
			{
				$gids = $group_user->get_user_groups($add_user);
				include_once('../admin/include/user.class.php');
				$user = new user();
				$user->edit_user(array('groups'=>implode(',',$gids)),$add_user);
				$response->alert('添加成功！');
				$response->script('self.location=self.location');
				
			}
			$response->execute();
		}
		break;
	case 'delall';
		if(!isset($view_list[$gid]) || check_perms('G006',$view_list[$gid])===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->redirect($base_dir);
			$response->execute();
			
		}
		if(isset($_POST['ck']))
		{
			include_once('../admin/include/user.class.php');
			$user = new user();
			foreach ($ck as $uid)
			{
				$group_user->del_user($uid,$gid);
				$gids = $group_user->get_user_groups($uid);
				$user->edit_user(array('groups'=>implode(',',$gids)),$uid);
			}
			$response->alert('删除成功！');
			$response->script('self.location=self.location');
			$response->execute();
		}
		else 
		{
			$response->alert('请选择要删除的管理员');
			$response->execute();
		}
		break;
	case 'del':
		if(!isset($view_list[$gid]) || check_perms('G006',$view_list[$gid])===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->redirect($base_dir);
			$response->execute();
		}
		$res = $group_user->del_user($uid,$gid);
		if($res == false)
		{
			$response->alert('删除失败');
		}
		else 
		{
			$gids = $group_user->get_user_groups($uid);
			include_once('../admin/include/user.class.php');
			$user = new user();
			$user->edit_user(array('groups'=>implode(',',$gids)),$uid);
			$response->alert('删除成功');
			$response->script('self.location=self.location');
		}
		$response->execute();
		break;
	case 'roles':
		if(!isset($view_list[$gid]) || check_perms('G007',$view_list[$gid])===false)
		{
			if(isset($edufepm) && $edufepm=='iframe')
			{
				$response->alert('您没有权限进行此操作！');
				$response->redirect($base_dir);
				$response->execute();
			}
			else 
			{
				showmessage('您没有权限进行此操作！');
			}
		}
		include_once '../admin/include/role.class.php';
		if(isset($_POST['u_id']))
		{
			$role = new role();
			$role_list = $role->get_all_role_list();
			if(!is_array($role_list) || sizeof($role_list)<=0)
			{
				$response->alert('暂无角色，不能设置此项');
				$response->execute();
			}
			include_once '../admin/include/user.class.php';
			$user = new user();
			$info = $user->get_user($u_id);
			if(!isset($info['userid']))
			{
				$response->alert('您要修改的用户不存在');
				$response->execute();
			}
			$flag = $group_user->check_user_in_group($u_id,$gid);
			if($flag==false)
			{
				$response->alert('您要修改的用户不在本组');
				$response->execute();
			}
			$group_user_role = new group_user_role();
			$group_user_role->del_user_group_role($u_id,$gid);
			if($r_flag==1 && is_array($roles) && sizeof($roles) > 0)
			{
				foreach ($roles as $v)
				{
					$group_user_role->add_user_group_role($u_id,$gid,$v);
				}
			}
			$response->alert('修改成功');
			$response->script('self.location=self.location');
			$response->execute();
			
		}
		else 
		{
			$role = new role();
			$role_list = $role->get_all_role_list();
			if(!is_array($role_list) || sizeof($role_list)<=0)
			{
				showmessage('暂无角色，不能设置此项');
			}
			include_once '../admin/include/user.class.php';
			$user = new user();
			$info = $user->get_user($u_id);
			if(!isset($info['userid']))
			{
				showmessage('您要修改的用户不存在');
			}
			$flag = $group_user->check_user_in_group($u_id,$gid);
			if($flag==false)
			{
				showmessage('您要修改的用户不在本组');
			}
			$group_user_role = new group_user_role();
			$roles = $group_user_role->get_user_group_role($u_id,$gid);
			include template('admin', 'group_user_roles');
		}
		break;
	case 'check':
		include_once '../admin/include/user.class.php';
		$user = new user();
		$uid = $user->get_userid($word);
		if(empty($uid))
		{
			$uid = 0;
		}
		$response->script('$("select[name=add_user]").val('.$uid.').selected=true;');
		$response->execute();
		break;
	case 'manage':
	default:
		$page = isset($page) ? intval($page) : 1;
		$pagesize = 20;
		$list = $group_user->get_group_user_info($gid,$page,$pagesize);
		$pages = $group_user->pages;
		$back_url = $forward;
		$forward = make_forward();
		include_once '../admin/include/user.class.php';
		$user = new user();
		$user_list = $user->get_all_user_list();
		include template('admin', 'group_users');
}
?>